/**
 * @Company: 小明
 * @Author: 40313
 * @DATE: 2020/12/18 星期五 下午 11:02
 */
package com.situ.oa.util;

import com.situ.oa.user.pojo.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * ClassName: ServletUtil
 *
 * @author 40313
 * @Description:(ServletUtil)
 */

public class ServletUtil {
    /**
     * @param session
     * @return
     * @Title: getUserCodeBySession
     * @Description:(从session中得到当前登录人的账号)
     */
    public static String getUserCodeBySession(HttpSession session) {
        User user = getUserBySession(session);
        if (user != null) {
            return user.getLoginAccount();
        }
        return null;
    }

    /**
     * @param session
     * @return
     * @Title: getUserBySession
     * @Description:(从session中得到登录用户的实例)
     */
    public static User getUserBySession(HttpSession session) {
        Object object = session.getAttribute(ConfigUtil.SESSION_LOGIN_USER_NAME);
        if (object != null) {
            return (User) object;
        }
        return null;
    }

    /**
     * 获得用户的登录IP
     *
     * @param request
     * @return
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if ("0:0:0:0:0:0:0:1".equals(ip)) {
            ip = "本地";
        }
        return ip;
    }

}
